"""
获取站端log并解析功能暂时不使用，采用召测获取返回报文的形式进行解析
"""

import re
import time
import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


def ssh_exe_cmd(cmd):
    """
    执行单条cmd
    :param cmd:
    :return:
    """
    global ssh
    stdin, stdout, stderr = ssh.exec_command(cmd)
    output_str = stdout.read().decode('utf-8')
    return output_str


def channel_exe_cmd(channel, cmd, end_flag="# "):
    """
    连续执行cmd
    :param channel:
    :param cmd:
    :param end_flag:
    :return:
    """
    channel.send(cmd)
    channel.send("\n")
    time.sleep(0.1)
    buff = ''
    while not buff.endswith(end_flag):
        resp = channel.recv(9999)
        # 出现异常字符时忽略
        buff += resp.decode('utf-8', errors='ignore')
        # print(buff)
    return buff


# def channel_exe_cmd_log(channel, cmd):
#     channel.send(cmd)
#     channel.send("\n")
#     time.sleep(0.1)
#     buff = ''
#
#     while channel.recv_ready():
#         # 读取数据
#         resp = channel.recv(9999)
#         # 出现异常字符时忽略
#         buff += resp.decode('utf-8', errors='ignore')
#         # print(buff)
#     return buff


def remove_color_codes(string):
    """
    正则表达式匹配ANSI颜色代码
    :param string:
    :return:
    """
    ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
    return ansi_escape.sub('', string)


# def get_log(ip, q):
#     """
#     获取十秒内log中是否含有启用插件4的描述
#     """
#     grep_data = ""
#     ssh.connect(hostname=ip, port=10088, username="oetsky", password="oetsky@87942")
#
#     try:
#         output_str = ssh_exe_cmd("sudo /home/oetsky/data/bin/chpwd --debug")
#         # print(output_str)
#         su_password = output_str[-7:]
#         # print(su_password)
#
#         channel = ssh.invoke_shell()
#         channel.settimeout(10)  # 这里将超时时间设置为20秒
#         time.sleep(0.1)
#
#         channel_exe_cmd(channel, "su", end_flag='密码：')
#         channel_exe_cmd(channel, su_password)
#
#         grep_data_all = ""
#         # 执行获取698的日志
#         for _ in range(5):
#             grep_data = channel_exe_cmd_log(channel,
#                                             "tail -f /opt/data/oetsky/data/project/cvt_client_8082/target/logs/SystemOut.log | grep -E DLT-698")
#             # print(grep_data)
#             time.sleep(0.5)
#
#             grep_data_all += grep_data
#         grep_data = remove_color_codes(grep_data_all)
#     except paramiko.ssh_exception.AuthenticationException:
#         print('paramiko.ssh_exception.AuthenticationException')
#     finally:
#         send_data = None
#         recv_data = None
#         match_data_send = re.findall(r"\[命令帧]\[冻结数据]：.*?\n", grep_data)
#         match_data_rec = re.findall(r"\[应答帧]\[冻结数据]\[.*?]\[.*?]:.*?\n", grep_data)
#         if match_data_send:
#             send_data = match_data_send[-1].split("：")[-1].strip()
#         if match_data_rec:
#             recv_data = match_data_rec[-1].split(":")[-1].strip()
#         q.put({"send_data": send_data, "recv_data": recv_data})


if __name__ == '__main__':
    pass
